<?php
// +----------------------------------------------------------------------
// |系统设置基本设置控制器
// +----------------------------------------------------------------------
// |Data:2015-02-28 11:19
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;

class BaseController extends AdminCommonController{
    private $_model;
    public function _initialize(){
        parent::_initialize();
        $this->_model = D('Base');
    }

    /**
    *获取列表
    */
    public function index(){
        if(IS_POST){
            $data = $this->getPostData();
            if(empty($data) || !is_array($data)){
                $this->returnData('提交数据错误！');
            }

            if($this->_model->update($data)){
                $this->clearCache();
                $this->addHistoryLog('保存基本配置成功！');
                $this->returnData('保存基本配置成功！',1);
            }else{
                $this->returnData('保存失败！');
            }
        }else{
            $info = $this->_model->detail(array('key'=>'base_config'));
            if($info){
                $val = unserialize($info['value']);
                if($val['qq']){
                    $qqArr = unserialize($val['qq']);
                    $this->assign('qqList',$qqArr);
                }
                $this->assign('val',$val);
            }
            $this->assign('info',$info);
            $this->display();
        }
    }

    /**
     * 获取表单数
     * @return array
     */
    private function getPostData(){
        $id = I('post.id');
        $list = I('post.list','','trim');
        $key = I('post.key','','trim');
        $pc_status = I('post.pc_status',1);
        $wx_status = I('post.wx_status',1);
        $sign_time = I('post.sign_time');//自动签收时间

        //数据判断
        $data = array(
            'pc_status'=>$pc_status,
            'wx_status'=>$wx_status
        );
        if($sign_time){
            if(!checkRegx("/^[0-9]*/",$sign_time)) $this->returnData('签收时间格式错误!');
            $data['sign_time'] = $sign_time;
        }

        $value = serialize($data);
        unset($data);

        $data = array(
            'list'=>$list,
            'key'=>$key,
            'value'=>$value,
            'update_time'=>NOW_TIME,
        );

        if($id){
            $data['id'] = $id;
        }
        return $data;
    }

    /**
     * 清理缓存
     */
    private function clearCache(){
        S(C('DB_PREFIX').'_admin_base_config',NULL);
    }
}
?>